package com.zhongxi.guoyuan.view.rewardCoin;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.viewpagerindicator.IcsLinearLayout;
import com.viewpagerindicator.TabPageIndicator;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.rewardCoin.RewardMyAdapter;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.projectItem.ProjectItemData;
import com.zhongxi.guoyuan.utils.CommonTool;

import java.util.List;

/**
 * Created by Administrator on 2017/9/27.
 */

public class RewardNewCategoryActivity extends BaseActivity {

    private ViewPager viewPager;
    private TabPageIndicator indicator;
    private int position;
    private List<ProjectItemData> projectItemDatas;
    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_new_category);
        Bundle bundle = getIntent().getExtras();
        projectItemDatas = (List<ProjectItemData>) bundle.getSerializable("category");
        position = bundle.getInt("position");
        initTitle();
        indicator = (TabPageIndicator) findViewById(R.id.indicator);
        viewPager = (ViewPager) findViewById(R.id.pager);
        tvTitle.setText("赚美豆超市");
        RewardMyAdapter adapter=new RewardMyAdapter(context,projectItemDatas);
        viewPager.setAdapter(adapter);
        indicator.setViewPager(viewPager);
        indicator.setCurrentItem(position);
        ((TextView) ((IcsLinearLayout) indicator
                .getChildAt(0))
                .getChildAt(position))
                .setTextColor(context.getResources().getColor(R.color.colorPrimary));
        ((TextView) ((IcsLinearLayout) indicator
                .getChildAt(0))
                .getChildAt(position)).setBackgroundResource(R.drawable.bg_select_gold);
        int spaceWith = CommonTool.dip2px(context, 20);//左边距 paddingLeft
        int with = getResources().getDisplayMetrics().widthPixels / 8;
        for (int i = 0; i < projectItemDatas.size(); i++) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, -2);
            params.setMargins(with - spaceWith, 0, 0, 0);
            ((TextView) ((IcsLinearLayout) indicator
                    .getChildAt(0))
                    .getChildAt(i)).setLayoutParams(params);
            ((TextView) ((IcsLinearLayout) indicator
                    .getChildAt(0))
                    .getChildAt(i)).setPadding(CommonTool.dip2px(context, 20), CommonTool.dip2px(context, 5), CommonTool.dip2px(context, 20), CommonTool.dip2px(context, 5));
            ((TextView) ((IcsLinearLayout) indicator
                    .getChildAt(0))
                    .getChildAt(i)).setTextSize(14);
        }

        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < projectItemDatas.size(); i++) {
                    if (i == position) {
                        ((TextView) ((IcsLinearLayout) indicator
                                .getChildAt(0))
                                .getChildAt(position))
                                .setTextColor(context.getResources().getColor(R.color.colorPrimary));
                        ((TextView) ((IcsLinearLayout) indicator
                                .getChildAt(0))
                                .getChildAt(position)).setBackgroundResource(R.drawable.bg_select_gold);
                    } else {
                        ((TextView) ((IcsLinearLayout) indicator
                                .getChildAt(0))
                                .getChildAt(i))
                                .setTextColor(context.getResources().getColor(R.color.text_hint_color));
                        ((TextView) ((IcsLinearLayout) indicator
                                .getChildAt(0))
                                .getChildAt(i))
                                .setBackgroundResource(R.color.background);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}
